/*
 * @Author: harity
 * @Date: 2021-07-28 16:46:27
 * @LastEditTime: 2021-07-28 17:16:12
 * @LastEditors: Please set LastEditors
 * @Description: 防抖
 * @FilePath: /learn-js/src/debounce.js
 */

/**
 * @description: 防抖，在事件被触发n秒后再执行回调，如果在这n秒内又被触发，则重新计时
 * @param {*} func 回掉函数
 * @param {*} wait 延迟执行的时间
 * @return {*}
 */
function debounce(func, wait = 500) {
    let timer = 0;
    return function (...arg) {
        if (timer) {
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            func.apply(this, arg)
        }, wait)

    }
}
console.log(111);
const fun1 = debounce(() => {
    console.log(123)
}, 2000)
fun1()
fun1()
fun1()
fun1()
fun1()
fun1()